package com.jiechuang.user.controller;


import com.github.pagehelper.PageInfo;
import com.jiechuang.common.annotation.JWTPermission;
import com.jiechuang.common.bean.Permission;
import com.jiechuang.common.bean.addOrUpdate.PermissionAddOrUpdate;
import com.jiechuang.common.web.Code;
import com.jiechuang.common.web.Response;
import com.jiechuang.user.dto.PermissionQueryInfo;
import com.jiechuang.user.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/permissions")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @PostMapping("/list")
    public Response permissionList(@RequestBody PermissionQueryInfo permissionQueryInfo){
        try {
            PageInfo<Permission> pageInfo = permissionService.getPermissionList(permissionQueryInfo);
            return new Response(Code.SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PostMapping("/permission")
    public Response addPermission(@RequestBody PermissionAddOrUpdate permissionAddOrUpdate){
        try {
             permissionService.addPermission(permissionAddOrUpdate);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SAVE_ERROR);
        }
    }
    @DeleteMapping("/{id}")
    public Response deletePermission(@PathVariable Integer id){
        try {
            permissionService.deletePermission(id);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PutMapping("/permission")
    public Response updatePermission(@RequestBody PermissionAddOrUpdate permissionAddOrUpdate){
        try {
            permissionService.updatePermission(permissionAddOrUpdate);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @GetMapping("/{id}")
    public Response getPermission(@PathVariable Integer id){
        try {
            Permission permission=permissionService.getPermission(id);
            return new Response(Code.SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }


}
